home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Celestin Apprentice 5
/
Apprentice-Release5.iso
/
Source Code
/
Libraries
/
C++ Toolbox 1.0
/
Memory++.h
< prev
next >
Wrap
C/C++ Source or Header
|
1996-03-09
|
3KB
|
175 lines
#pragma once
/*
File: Memory++.h
Contents: C++ extensions to the Macintosh interfaces.
Version: 1.0.1 (for System 7.x)
Copyright: ©1995 Chris K. Thomas. All Rights Reserved.
1.0.1 ckt December 15, 1995 add direct handle accessor methods (sethandle/gethandle)
*/
#include "Types++.h"
//
// A wrapper class for arrays contained within
// Mac handles; purposely a very light runtime
// implementation.
//
template <class T>
class HandleArray
{
Boolean weOwnHandle;
T **ourHandle;
public:
HandleArray(T** inHandle)
{
ourHandle = inHandle;
weOwnHandle = false;
}
HandleArray()
{
ourHandle = reinterpret_cast<T **>(NewHandle(0));
if(ourHandle == NULL)
Throw_(MemError());
weOwnHandle = true;
}
HandleArray(long inCount)
{
ourHandle = reinterpret_cast<T **>(NewHandleClear(sizeof(T) * inCount));
if(ourHandle == NULL)
Throw_(MemError());
weOwnHandle = true;
}
~HandleArray()
{
if(ourHandle && *ourHandle && weOwnHandle)
{
DisposeHandle(*this);
}
}
// • subscript operator
// allows access as a first-class array
T & operator[](long inIndex)
{
if(!(inIndex >= 0 && inIndex < getcount())) // bounds checking
{
DebugNum(inIndex);
DebugNum(getcount());
Throw_('asrt');
}
return (*ourHandle)[inIndex];
}
// * conversion operators
operator Handle()
{
return reinterpret_cast<Handle>(ourHandle);
}
operator T**()
{
return reinterpret_cast<T**>(ourHandle);
}
// * accessors
void lock()
{
HLockHi(reinterpret_cast<Handle>(ourHandle));
}
void unlock()
{
HUnlock(reinterpret_cast<Handle>(ourHandle));
}
void sethandle(T **inNewHandle)
{
ourHandle = inNewHandle;
}
T **gethandle()
{
return ourHandle;
}
void setcount(SInt32 inCount)
{
Assert_(inCount >= 0);
char handleState;
// DebugNum(sizeof(T));
handleState = HGetState(reinterpret_cast<Handle>(ourHandle));
unlock();
SetHandleSize(reinterpret_cast<Handle>(ourHandle), inCount * sizeof(T));
long ourErr = MemError();
if(ourErr != noErr)
{
HSetState(reinterpret_cast<Handle>(ourHandle), handleState);
Throw_(ourErr);
}
HSetState(reinterpret_cast<Handle>(ourHandle), handleState);
}
SInt32 getcount()
{
return (GetHandleSize(reinterpret_cast<Handle>(ourHandle)) / sizeof(T));
}
};
//
// wrapper class for a standard pointer
//
template <class T>
class Pointer
{
public:
Boolean own;
T *ptr;
Pointer(Size inSize)
{
ptr = (T *)NewPtrClear(inSize);
if(ptr == NULL)
Throw_(MemError());
own = true;
}
Pointer(T *inPtr)
{
ptr = inPtr;
own = false;
}
~Pointer()
{
if(own)
{
DisposePtr((Ptr) ptr);
}
}
operator T *()
{
return ptr;
}
operator Ptr()
{
return (Ptr)ptr;
}
};